<?php

/**
 * Created by PhpStorm.
 * User: shwy
 * Date: 2017/9/5
 * Time: 10:36
 * 供应商管理相关接口
 */
class supplier extends CI_Controller
{


    public function __construct()
    {
        parent::__construct();
        $this->load->model('supplier_model');

    }



    //选择供应商列表接口
    public function supplierList()
    {
        $page_num = $this->config->item('fenye')['pageNum']; //每页的数据
        $start_position = ''; //初始数据
        $page = $_POST['page']; //获取页码数量
        if(!$page){
            $num = 1;
            $start_position = ($num - 1) * $page_num;
        }else{
            $start_position = ($page - 1) * $page_num;
        }

        //判断搜索内容是否存在存在就继续
        if($this->input->get('search')){
            $searchArg = [
                'field' => 'name',
                'arg'=> $_POST['search'],
                'side' => 'both'
            ];

            $data = $this->supplier_model->search('*',0,$searchArg,$page_num,$start_position);
            $viewData = [
                'list' => $data[0],  //这里得到从数据库中的当页数据
                'totalNum' => $data['total_num'], //这里得到从数据库中的总页数
                'pageNum'  => $page_num //每页显示多少条
            ];

            echo json_encode($viewData,JSON_UNESCAPED_UNICODE);
        } else {

            $data = $this->supplier_model->page('supplier',0,$page_num,$start_position); //limit查询的数据
            $viewData = [
                'list' => $data[0],  //这里得到从数据库中的当页数据
                'totalNum' => $data['total_num'], //这里得到从数据库中的总页数
                'pageNum'  => $page_num //每页显示多少条

            ];

            echo json_encode($viewData,JSON_UNESCAPED_UNICODE);
        }
    }


    //供应商筛选接口
    public function search(){

//供应商筛选条件
//开始时间：startTime
//结束时间：endTime
//规模：size
//省：province
//县：city
//区：district
//搜索：name

        $argc = [];
        if(isset($_GET['startTime'])  && $_GET['startTime']){
            $argc += [
                'createTime >'=> strtotime($_GET['startTime']),
            ];
        }

        if(isset($_GET['endTime'])  && $_GET['endTime']){
            $argc += [
                'createTime <'=> strtotime($_GET['endTime']),
            ];
        }

//        echo '<pre>';
//        print_r($argc);
//        die;

        if(isset($_GET['province']) && $_GET['province']){
            $argc += [
                'province' => $_GET['province']
            ];
        }

        if(isset($_GET['city']) && $_GET['city']){
            $argc += [
                'city' => $_GET['city']
            ];
        }


        if(isset($_GET['district']) && $_GET['district']){
            $argc += [
                'district' => $_GET['district']
            ];
        }


        //规模大小
        if(isset($_GET['size']) && $_GET['size']){
            $argc += [
                'size' => $_GET['size']
            ];
        }


        if(isset($_GET['search']) && $_GET['search']){
            $searchArg = [
                'field' => 'name',
                'arg'=> $_GET['search'],
                'side' => 'both'
            ];
        }else{
            $searchArg = '';
        }


//        echo '<pre>';
//        print_r($argc);
//        print_r($searchArg);
//        die;


        $page_num = $this->config->item('fenye')['pageNum']; //每页的数据
        $start_position = ''; //初始数据
        $page = ($this->input->get('page')); //获取页码数量
        if(!$page){
            $num = 1;
            $start_position = ($num - 1) * $page_num;
        }else{
            $start_position = ($page - 1) * $page_num;
        }

        if(!$argc){
            $argc = 0;
        }



        if($searchArg){

            $supplier = $this->supplier_model->search('*',$argc,$searchArg,$page_num,$start_position);

        }else{
            $supplier = $this->supplier_model->page('supplier',$argc,$page_num,$start_position);
        }

        

        foreach ($supplier[0] as $k=>$v){
            $supplier[0][$k]['createTime'] = date('Y-m-d',$v['createTime']);
        }



        $supplier['pageNum'] = $page_num;
        $supplier['this_page'] = $page;




//        echo '<pre>';
//        print_r($supplier);
//        die;
        echo json_encode($supplier,JSON_UNESCAPED_UNICODE);

    }


}










































